import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter_juwo/ui/custom/real_rich_text.dart';
import 'package:flutter_juwo/ui/pages/personalCenterPages/ts/home/line_chart_sample4.dart';
import 'package:flutter_juwo/ui/utils/progress_view.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';
import 'package:flutter_echarts/flutter_echarts.dart';
import 'package:fl_chart/fl_chart.dart';

class HomeView {
  ///平台活动
  static activity(var data) {
    List<Widget> list = [];
    list.add(_title("平台活动"));
    for (var item in data) {
      list.add(_activityItem(item));
    }

    return Container(
      child: Column(
        children: list,
      ),
    );
  }

  ///邀请设置
  static shareSetting(String url) {
    return Container(
      child: Column(
        children: <Widget>[
          Container(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                _title("邀请设置"),
                InkWell(
                  onTap: () {},
                  child: Container(
                    padding: UiUtils.edgeOnly(l: 30, r: 30),
                    child: UiUtils.t("推广人管理", size: 24, color: Colours.cmain),
                  ),
                )
              ],
            ),
          ),
          Container(
            width: ScrUtil.w(750),
            margin: UiUtils.edgeOnly(l: 30, r: 30),
            padding: UiUtils.edgeOnly(l: 30, r: 30, t: 30, b: 30),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              children: <Widget>[
                Container(
                  child: UiUtils.t("邀请链接",
                      size: 30,
                      color: Colours.cTextTitle,
                      font: Font.PingFangBlod),
                ),
                Container(
                  height: ScrUtil.w(70),
                  decoration: BoxDecoration(
                      color: Colours.cbg,
                      borderRadius: BorderRadius.circular(16)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Container(
                        width: ScrUtil.w(480),
                        padding: UiUtils.edgeOnly(l: 10),
                        child: UiUtils.t(url,
                            size: 24,
                            color: Color.fromARGB(255, 114, 122, 141)),
                      ),
                      InkWell(
                        onTap: () {
                          ClipboardData data = new ClipboardData(text: "$url");
                          Clipboard.setData(data);
                          ToastUtil.showShortToast("已复制");
                        },
                        child: Container(
                          margin: UiUtils.edgeOnly(r: 10),
                          height: ScrUtil.w(64),
                          width: ScrUtil.w(140),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: Color.fromARGB(255, 253, 172, 61),
                              borderRadius: BorderRadius.circular(16)),
                          child: UiUtils.t("复制", size: 28, color: Colors.white),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: UiUtils.edgeOnly(t: 30, b: 30),
                  alignment: Alignment.centerLeft,
                  child: UiUtils.t("推广方式",
                      color: Colours.cTextTitle, font: Font.PingFangBlod),
                ),
                Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      _pushType("指定人员可以邀请"),
                      _pushType("所有会员都可邀请"),
                    ],
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  //做单统计
  /// xData x轴显示的
  /// series 对应的值
  static census2(var orderData, var peopleData) {
    return Container(
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              _title("做单统计"),
              Container(
                margin: UiUtils.edgeOnly(l: 12),
                width: ScrUtil.w(120),
                height: ScrUtil.w(32),
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color.fromARGB(255, 255, 248, 232),
                    borderRadius: BorderRadius.circular(16)),
                child: UiUtils.t("2020",
                    size: 24, color: Color.fromARGB(255, 255, 172, 0)),
              ),
            ],
          ),
          Container(
            height: ScrUtil.w(442),
            decoration: BoxDecoration(
                color: Colors.white, borderRadius: BorderRadius.circular(10)),
            margin: UiUtils.edgeOnly(l: 30, r: 30),
            child: Column(
              children: <Widget>[
                Container(
                  padding: UiUtils.edgeOnly(l: 30, r: 30),
                  decoration: BoxDecoration(
                      color: Colours.cbg2,
                      borderRadius:
                          BorderRadius.vertical(top: Radius.circular(10))),
                  height: ScrUtil.w(60),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: <Widget>[
                      Container(
                        child: UiUtils.t("2020-07-07",
                            size: 28, color: Colours.cText0),
                      ),
                      Container(
                        child: RealRichText([
                          TextSpan(
                              text: "共",
                              style: TextStyles.custom(
                                  28, Colours.cText0, Font.PingFangMedium)),
                          TextSpan(
                              text: "12",
                              style: TextStyles.custom(
                                  28, Colours.cText4, Font.PingFangMedium)),
                          TextSpan(
                              text: "人累计做单",
                              style: TextStyles.custom(
                                  28, Colours.cText0, Font.PingFangMedium)),
                          TextSpan(
                              text: "20",
                              style: TextStyles.custom(
                                  28, Colours.cText4, Font.PingFangMedium)),
                          TextSpan(
                              text: "件",
                              style: TextStyles.custom(
                                  28, Colours.cText0, Font.PingFangMedium)),
                        ]),
                      )
                    ],
                  ),
                ),
                Container(
                  padding: UiUtils.edgeOnly(l: 0, r: 30, t: 40, b: 20),
                  child: LineChartSample4(orderData, peopleData),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  static census(var xData, var series) {
    String option = '''
    {
      tooltip: {
          trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        data: ${jsonEncode(xData)},
        axisLabel: {show: true,
                      textStyle: {
                        color: '#A6B0BE',
                        fontSize:'12'
                      }
        },
         axisLine:{
          lineStyle:{
              color:'#f7f8fb',
               type:'dashed'  
          }
         }
      },
      yAxis: {
        type: 'value',
        axisLabel: {show: true,
                    textStyle: {
                      color: '#001745',
                      fontSize:'12'
                    }
        },
        axisLine:{
            lineStyle:{
                color:'#fff'
            }
        },
        splitLine :{    //网格线
        lineStyle:{
         color:'#f7f8fb',
         type:'dashed'    //设置网格线类型 dotted：虚线   solid:实线
        },
        show:true //隐藏或显示
    }

      },
      color:['#FC8B1C'],
      series: [{
        data: ${jsonEncode(series)},
        type: 'line',
        smooth: true
      }]
    }
  ''';
    List<Map<String, Object>> _data1 = [
      {'name': 'Please wait', 'value': 0}
    ];
    return Container(
      child: Echarts(
          option: option,
          extraScript: '''
                    chart.on('click', (params) => {
                      if(params.componentType === 'series') {
                        Messager.postMessage(JSON.stringify({
                          type: 'select',
                          payload: params.dataIndex,
                        }));
                      }
                    });
                  ''',
          onMessage: (String message) {
            Map<String, Object> messageAction = jsonDecode(message);
            print(messageAction);
            if (messageAction['type'] == 'select') {
              final item = _data1[messageAction['payload']];
              print("------");
              print(item['name'].toString());
              // _scaffoldKey.currentState.showSnackBar(
              //     SnackBar(
              //       content: Text(item['name'].toString() + ': ' + display(item['value'])),
              //       duration: Duration(seconds: 2),
              //     ));
            }
          }),
      width: 300,
      height: 250,
    );
  }

  //自定义titlebar
  static titleBar(String title, String url, String name) {
    return Container(
      height: ScrUtil.w(88),
      color: Colors.white,
      margin: EdgeInsets.only(top: Utils.getAppBarTop()),
      padding: UiUtils.edgeOnly(l: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Container(
              alignment: Alignment.centerLeft,
              child: UiUtils.t(title,
                  size: 36, font: Font.PingFangBlod, color: Colours.appC2),
            ),
          ),
          Container(
            //padding: UiUtils.edgeOnly(left: 10, right: 10, top: 10, bottom: 10),
            child: ClipOval(
              child: CachedNetworkImage(
                width: ScrUtil.w(50),
                height: ScrUtil.w(50),
                fit: BoxFit.fill,
                imageUrl: url,
                placeholder: (context, url) => new ProgressView(),
                errorWidget: (context, url, error) => Image.asset(
                  Utils.getImgPath('icon_default_head'),
                  fit: BoxFit.fill,
                ),
              ),
            ),
          ),
          Container(
            alignment: Alignment.center,
            padding: UiUtils.edgeOnly(
              l: 20,
              r: 30,
            ),
            child: UiUtils.t(name, color: Color.fromARGB(255, 0, 6, 55)),
          )
        ],
      ),
    );
  }

  //看板数据
  static kanban({
    var money, //账户余额
    var unReceivedMoney, //预估未到账收入
    var allMoney, //累计收入
    var inviteNum, //累计邀请
    var makeNum, //做单人数
    var makeRate, //做单比例
    var makeNum7, //七日做单人数
    var makeRate7, //七日做单率
    var unMakeNum, //从未做单人数
    var unMakeRate, //从未做单率
  }) {
    return Column(
      children: <Widget>[
        Container(
          height: ScrUtil.w(239),
          width: ScrUtil.w(690),
          margin: UiUtils.edgeOnly(t: 30),
          decoration: BoxDecoration(
              image: DecorationImage(
            image: AssetImage(Utils.getImgPath("push_home_bg")),
          )),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                padding: UiUtils.edgeOnly(t: 30, l: 30),
                child: RealRichText([
                  TextSpan(
                      text: "￥",
                      style: TextStyles.custom(
                          36, Colors.white, Font.PingFangMedium)),
                  TextSpan(
                      text: "$money",
                      style: TextStyles.custom(
                          60, Colors.white, Font.PingFangBlod)),
                  TextSpan(
                      text: " 推广收入（近30天）",
                      style: TextStyles.custom(
                          24, Colors.white, Font.PingFangMedium))
                ]),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        padding: UiUtils.edgeOnly(t: 10, l: 30),
                        child: RealRichText([
                          TextSpan(
                              text: "预估未到账收入:￥",
                              style: TextStyles.custom(
                                  24,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangMedium)),
                          TextSpan(
                              text: "$unReceivedMoney",
                              style: TextStyles.custom(
                                  28,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangRegular))
                        ]),
                      ),
                      Container(
                        padding: UiUtils.edgeOnly(t: 10, l: 30),
                        child: RealRichText([
                          TextSpan(
                              text: "累计收入:￥",
                              style: TextStyles.custom(
                                  24,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangMedium)),
                          TextSpan(
                              text: "$allMoney",
                              style: TextStyles.custom(
                                  28,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangRegular))
                        ]),
                      )
                    ],
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        padding: UiUtils.edgeOnly(t: 10, r: 30),
                        child: RealRichText([
                          TextSpan(
                              text: "本月预估收益:￥",
                              style: TextStyles.custom(
                                  24,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangMedium)),
                          TextSpan(
                              text: "$unReceivedMoney",
                              style: TextStyles.custom(
                                  28,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangRegular))
                        ]),
                      ),
                      Container(
                        padding: UiUtils.edgeOnly(t: 10, r: 30),
                        child: RealRichText([
                          TextSpan(
                              text: "上月收益:￥",
                              style: TextStyles.custom(
                                  24,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangMedium)),
                          TextSpan(
                              text: "$allMoney",
                              style: TextStyles.custom(
                                  28,
                                  Color.fromARGB(255, 239, 245, 255),
                                  Font.PingFangRegular))
                        ]),
                      )
                    ],
                  )
                ],
              )
            ],
          ),
        ),
        Container(
          margin: UiUtils.edgeOnly(l: 30, r: 30),
          height: ScrUtil.w(360),
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.vertical(bottom: Radius.circular(10))),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  _kanbanItem("累计邀请", [
                    TextSpan(
                        text: "$inviteNum",
                        style: TextStyles.custom(36,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangBlod)),
                    TextSpan(
                        text: "人",
                        style: TextStyles.custom(20,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangMedium))
                  ]),
                  _kanbanItem("直接邀请", [
                    TextSpan(
                        text: "$inviteNum",
                        style: TextStyles.custom(36,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangBlod)),
                    TextSpan(
                        text: "人20%",
                        style: TextStyles.custom(20,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangMedium))
                  ]),
                  _kanbanItem("间接邀请", [
                    TextSpan(
                        text: "321",
                        style: TextStyles.custom(36,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangBlod)),
                    TextSpan(
                        text: "人20%",
                        style: TextStyles.custom(20,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangMedium))
                  ])
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  _kanbanItem("累计做单人数", [
                    TextSpan(
                        text: "$makeNum7",
                        style: TextStyles.custom(36,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangBlod)),
                    TextSpan(
                        text: "人$makeRate7%",
                        style: TextStyles.custom(20,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangMedium))
                  ]),
                  _kanbanItem("7日做单率", [
                    TextSpan(
                        text: "$makeNum7",
                        style: TextStyles.custom(36,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangBlod)),
                    TextSpan(
                        text: "人$makeRate7%",
                        style: TextStyles.custom(20,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangMedium))
                  ]),
                  _kanbanItem("从未做单人数", [
                    TextSpan(
                        text: "$unMakeNum",
                        style: TextStyles.custom(36,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangBlod)),
                    TextSpan(
                        text: "人$unMakeRate%",
                        style: TextStyles.custom(20,
                            Color.fromARGB(255, 0, 6, 55), Font.PingFangMedium))
                  ]),
                ],
              )
            ],
          ),
        )
      ],
    );
  }

  //看板项
  static _kanbanItem(String txt, List<TextSpan> textSpanList) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Container(
            child: RealRichText(
              textSpanList,
              textAlign: TextAlign.center,
            ),
          ),
          Container(
            margin: UiUtils.edgeOnly(t: 20),
            child: UiUtils.t(txt,
                size: 24, color: Color.fromARGB(255, 171, 182, 195)),
          ),
        ],
      ),
    );
  }

  static _title(String txt) {
    return Container(
      height: ScrUtil.w(88),
      margin: UiUtils.edgeOnly(l: 30),
      alignment: Alignment.centerLeft,
      child: UiUtils.t(txt,
          size: 34,
          color: Color.fromARGB(255, 0, 6, 55),
          font: Font.PingFangBlod),
    );
  }

  static _pushType(String s) {
    return Container(
      alignment: Alignment.center,
      width: ScrUtil.w(280),
      height: ScrUtil.w(66),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2),
          border: Border.all(color: Color.fromARGB(255, 245, 247, 250))),
      child: UiUtils.t(s, size: 28, color: Color.fromARGB(255, 0, 6, 55)),
    );
  }

  //               "url": "ima",
  //               "title": "直接邀请活动",
  //               "time": "2020/07/01至2020/07/20",
  //               "msg": "直接邀请好友做单，淘宝天猫单，直接邀请奖励0.5金..."
  //
  static _activityItem(data) {
    return InkWell(
      onTap: () {
        showModalBottomSheet(
            backgroundColor: Colors.transparent,
            context: Utils.appContext,
            builder: (builder) {
              return new Container(
                  height: ScrUtil.w(700),
                  decoration: new BoxDecoration(
                      color: Colors.white,
                      borderRadius: new BorderRadius.only(
                          topLeft: const Radius.circular(25.0),
                          topRight: const Radius.circular(25.0))),
                  child: _bottomShow(data));
            });
      },
      child: Container(
        margin: UiUtils.edgeOnly(l: 30, r: 30, b: 16),
        child: Row(
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: UiUtils.cachedNetworkImage(210, 210, data["url"]),
            ),
            Container(
              width: ScrUtil.w(470),
              margin: UiUtils.edgeOnly(l: 10),
              padding: UiUtils.edgeOnly(l: 16, t: 30, r: 16),
              height: ScrUtil.w(210),
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(10)),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Container(
                    child: UiUtils.t(data["title"],
                        size: 32,
                        color: Colours.cText0,
                        font: Font.PingFangBlod),
                  ),
                  Container(
                    margin: UiUtils.edgeOnly(t: 6, b: 6),
                    child: UiUtils.t("活动时间：${data["time"]}",
                        size: 24, color: Colours.cText1),
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: <Widget>[
                      Expanded(
                        child: Container(
                          alignment: Alignment.centerLeft,
                          child: UiUtils.t(data["msg"],
                              size: 24, color: Colours.cText1, maxLines: 2),
                        ),
                      ),
                      Container(
                        child: Image.asset(
                          Utils.getImgPath("icon_arr"),
                          width: ScrUtil.w(44),
                          height: ScrUtil.w(44),
                        ),
                      )
                    ],
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }

  static _bottomShow(data) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          //    "url":
          //                   "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602503176357&di=f90bb5b86e11ef2e90492e55c91103d4&imgtype=0&src=http%3A%2F%2Fwx3.sinaimg.cn%2Flarge%2F006VYTdfgy1g0hvon8hnij30ls0c9n9d.jpg",
          //               "title": "直接邀请活动",
          //               "time": "2020/07/01至2020/07/20",
          //               "msg": "直接邀请好友做单，淘宝天猫单，直接邀请奖励0.5金..."
          Container(
              alignment: Alignment.center,
              margin: UiUtils.edgeOnly(t: 60),
              child: UiUtils.t(data["title"],
                  size: 38, color: Colours.cText0, font: Font.PingFangBlod)),
          Container(
              margin: UiUtils.edgeOnly(t: 60, l: 30),
              child: RealRichText([
                TextSpan(
                    text: "活动时间:",
                    style: TextStyles.custom(
                        32, Colours.cText1, Font.PingFangMedium)),
                TextSpan(
                    text: " ${data["time"]}",
                    style: TextStyles.custom(
                        32, Colours.cText0, Font.PingFangMedium))
              ])),
          Container(
              margin: UiUtils.edgeOnly(t: 60, l: 30),
              child: UiUtils.t("活动内容", size: 32, color: Colours.cText1)),
          Container(
              margin: UiUtils.edgeOnly(t: 10, l: 30),
              child: UiUtils.t("${data["msg"]}", size: 32, color: Colours.cText0,maxLines: 3)),

        ],
      ),
    );
  }
}
